package com.milo.thread.create;


import com.milo.utils.DateUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author milo
 * @date 2019/4/2015:19
 * @description 线程池的方式创建多线程
 */
public class ThreadPool {

    public static void main(String[] args) {
        //创建一个可缓存的线程池
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4);
        for (int i = 0; i < 10; i++) {
            final int index = i;
            fixedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        System.out.println(index + " " + Thread.currentThread().getName() + " " + DateUtils.getNowDate());
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        fixedThreadPool.shutdown();
    }

}
